﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Script.Serialization;

namespace Ewts.ashx
{
    /// <summary>
    /// Tree 的摘要说明
    /// </summary>
    public class Tree : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string json = "";

            if (context.Request["no"] != null && context.Request["no"].ToString().Trim() != "")
            {
                string no = context.Request["no"].ToString().Trim();
                string notype = context.Request["notype"].ToString().Trim();
                json = getJsonData(no,notype);
            }
            else if(context.Request["date"] != null && context.Request["date"].ToString().Trim()!="")
            {
                string date = context.Request["date"].ToString();
                int id = int.Parse(context.Request["id"].ToString());
                int type = int.Parse(context.Request["type"].ToString());
                string oldate = context.Request["oldate"].ToString();
                if (updatedate(type, id, date, oldate) == true)
                {
                    json = "1";
                }
                else 
                {
                    json = "0";
                }
            }
            

            context.Response.Write(json);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public string getJsonData(string no,string notype)
        {
            string json = "";
            json = GetJsonSerail(no,notype);
            return json;
        }

        
        public string GetJsonSerail(string no,string notype)
        {
            DataTable dt = new DataLib.BLL.LoadNum().GetTreeVieww(no,notype);
            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<treeData> list = serialChildJson(dt, "0");
            string json = jss.Serialize(list);
            return json;
        }

        public List<treeData> serialChildJson(DataTable dt, string pid)
        {

            JavaScriptSerializer jss = new JavaScriptSerializer();
            List<treeData> list = new List<treeData>();
            if (dt == null || dt.Rows.Count ==0 )
                return null;
            DataRow[] rows = dt.Select("parid=" + pid);

            treeData model;
            if (rows != null && rows.Length > 0)
            {
                for (int i = 0; i < rows.Length; i++)
                {

                    model = new treeData();
                    model.id = int.Parse(rows[i]["rowid"].ToString());
                    model.name = getobjStr(rows[i]["loadnum"]);

                    model.selfno = getobjStr(rows[i]["selfno"]);
                    model.url = getobjStr(rows[i]["selfurl"]);
                    model.datetype = int.Parse(rows[i]["datetype"].ToString());
                    model.selfid = int.Parse(rows[i]["selfid"].ToString());
                    model.selftstatus = int.Parse(rows[i]["selfstatus"].ToString());
                    if (rows[i]["bardate"] != null)
                    {
                        model.bardate = rows[i]["bardate"].ToString();
                    }
                    if (rows[i]["createtime"] != null && rows[i]["createtime"].ToString().Trim() != "")
                    {
                        model.createtime = DateTime.Parse(rows[i]["createtime"].ToString()).ToString("yyyy-MM-dd");
                    }
                    else model.createtime = "";
                    model.nwt = decimal.Parse(rows[i]["nwt"].ToString());

                    if (dt.Select("parid=" + rows[i]["rowid"].ToString()).Length > 0)
                    {
                        model.children = serialChildJson(dt, rows[i]["rowid"].ToString());

                    }
                    list.Add(model);

                }

            }

            //序列化  
            return list;// jss.Serialize(list);
        }



        public string getobjStr(Object obj) 
        {
            if (obj != null)
            {
                return obj.ToString();
            }
            else { 
                return "";
            }
        }

        public bool updatedate(int type,int id,string date,string olddate) 
        {
            bool flag = false;
            DataLib.BLL.InStorage bins = new DataLib.BLL.InStorage();
            
            switch(type)
            {
                case 1:
                    DataLib.BLL.LoadNum bload = new DataLib.BLL.LoadNum();
                    DataLib.Model.LoadNum mload = bload.GetModel(id);
                    mload.createdate = GetNewTime(DateTime.Parse(date), (DateTime)mload.createdate);
                    flag = bload.Update(mload);
                    break;
                case 2:
                    DataLib.BLL.Cjo bcjo = new DataLib.BLL.Cjo();
                    DataLib.Model.Cjo mcjo = bcjo.GetModel(id);
                    mcjo.createtime = GetNewTime(DateTime.Parse(date), (DateTime)mcjo.createtime);
                    flag = bcjo.Update(mcjo);
                    break;
                case 3:
                    DataLib.Model.InStorage mins = bins.GetModel(id);
                    mins.instoragetime = GetNewTime(DateTime.Parse(date), (DateTime)mins.instoragetime);
                    flag = bins.Update(mins);
                    break;
                case 4:
                    DataLib.BLL.OutStorage bouts = new DataLib.BLL.OutStorage();
                    DataLib.Model.OutStorage mouts = bouts.GetModel(id);
                    mouts.outStorageDate = GetNewTime(DateTime.Parse(date), (DateTime)mouts.outStorageDate);
                    flag = bouts.Update(mouts);
                    break;
                case 5:
                    flag = bins.updateBarcodDate(id.ToString(), olddate, date);
                    break;
                default:
                    break;
            }
            return flag;
        }

        /// <summary>
        /// 为新日期加上原来的时分秒
        /// </summary>
        /// <param name="d1">新日期</param>
        /// <param name="d2">原日期</param>
        /// <returns></returns>
        private DateTime GetNewTime(DateTime d1, DateTime d2)
        {
            if (d1 != null && d2 != null)
            {
                d1.AddHours(d2.Hour);
                d1.AddMinutes(d2.Minute);
                d1.AddSeconds(d2.Second);
            }
            return d1;
        }

        
    }



    [Serializable]
    public partial class treeData
    {
        #region Model
        private int _id;
        private int _pId;
        private string _name;
        private decimal _nwt;
        private string _loadnum;
        private string _createtime;
        private string _url;
        private string _selfno;
        private int _selfid;
        private int _datetype;
        private string _bardate;
        private List<treeData> _children;
        private int _selfstatus;

        public int id
        {
            set { _id = value; }
            get { return _id; }
        }

        public int pId 
        {
            set { _pId = value; }
            get { return _pId; }
        }

        public string name
        {
            set { _name = value; }
            get { return _name; }
        }

        public decimal nwt
        {
            set { _nwt = value; }
            get { return _nwt; }
        }

        public string loadnum
        {
            set { _loadnum = value; }
            get { return _loadnum; }
        }

        public string url
        {
            set { _url = value; }
            get { return _url; }
        }

        public string selfno
        {
            set { _selfno = value; }
            get { return _selfno; }
        }

        public string createtime
        {
            set { _createtime = value; }
            get { return _createtime; }
        }

        public List<treeData> children {
            set { _children = value; }
            get { return _children; }
        }

        public int datetype {
            set { _datetype = value; }
            get { return _datetype; }
        }

        public int selfid {
            set { _selfid = value; }
            get { return _selfid; }
        }

        public string bardate {
            set { _bardate = value; }
            get { return _bardate; }
        }

        public int selftstatus {
            set {_selfstatus = value; }
            get { return _selfstatus; }
        }
        #endregion
    }
}